/**
 * 
 */
/***************************************************************
                           病人管理模块
***************************************************************/
var addTreatDialog;
function treatManagerInit()
{
	$("#treatTable tr:nth-child(even)").addClass("trevencolor");
	  BUI.use(['bui/overlay','bui/form'],function(Overlay,Form){
	      	 var form = new Form.HForm({
	        	 srcNode : '#form'
   	  }).render();
	 
	  addTreatDialog = new Overlay.Dialog({
          title:'添加患者诊断治疗记录',
          width:540,
          height:400,
          //配置DOM容器的编号
          contentId:'treat_content',
          cancel:function(){
	      	 choose_close();
	      	 clearTreatDialog();
	      },
          success:function () {
        	  saveOrUpdateTreat();
            }
        });
    });
	 
	 
    $("#add_Treat").bind("click",function(){
	    clearTreatDialog();
	    addTreatDialog.set("title","添加患者诊断治疗记录");
	    addTreatDialog.show();
	    choose_open();
     });
   
    $("#treat_startDate").css({cursor: 'pointer'});//手型光标
    $("#treat_endDate").css({cursor: 'pointer'});
    $("#treat_t_date").css({cursor: 'pointer'});
    $("#treat_startDate").click(function(){
		WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'});
    });
    $("#treat_endDate").click(function(){
		WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'});
    });
    $("#treat_t_date").click(function(){
		WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'});
    });
    
    var zlfsUrl = projectName + "/treat/getZlfsList.do?date=" + new Date().getTime();
	$.ajax({
		url: zlfsUrl,
		async: false,
		success: function(data,result){
			for(var i = 0; i < data.length; i++)
			{
				$("#treat_therary").append("<option>"+data[i]+"</option>");
			}
		}
	});
	
   treatValidate();
}

/**
 *	添加添加患者诊断治疗记录  id为空;
 *  编辑添加患者诊断治疗记录 
 */
function saveOrUpdateTreat()
{
    var id = $("#treat_id").val();
	if($("#treatForm").validate().form())
	{
		addTreatDialog.close();
		var url = projectName + "/treat/saveOrUpdate.do?postDate=" + new Date().getTime();
		$.ajax({
			url: url,
			type: "post",
			async: false,
			data: $('#treatForm').serialize(),
			 error: function(XMLHttpRequest, textStatus, errorThrown) {
				 alert(XMLHttpRequest.status);
				 alert(XMLHttpRequest.readyState);
				 alert(textStatus);
				   },
			success: function(data,result)
			{
				if(id == "" || id == null)
				{
					if("ERROR" == data)
					{
						messageAlert(1,"添加患者诊断治疗记录失败，请重试",function()
								{
							this.close();
							choose_close();
							showTreatPageDataList(1);
						});
						
					}
					else
					{
						messageAlert(1,"添加患者诊断治疗记录成功，病历号为："+data,function()
								{
									this.close();
									choose_close();
									showTreatPageDataList(1);
						});
					}
				}
				else
				{
					if("ERROR" == data){
						messageAlert(1,"修改患者诊断治疗记录失败，请重试",function()
								{
									this.close();
									choose_close();
									showTreatPageDataList(1);
						});
					}
					else
					{
						messageAlert(1,"修改患者诊断治疗记录成功",function()
								{
									this.close();
									choose_close();
									showTreatPageDataList(1);
						});
					}
					
				}
				
			}
		});
		
	}
}

/**
 *	清空表单数据 
 */
function clearTreatDialog(){
	treatFormValidate.resetForm();
	$("#treat_mrn").val("");
	$("#treat_eno").val("");
	$("#treat_therary").val("");
	$("#treat_t_date").val("");
	$("#treat_memo").val("");
	$("#treat_id").val("");
}


/**
 *	删除患者诊断治疗记录 
 */
function deleteTreat(id)
{
	$.ajax({
		 url: projectName+"/treat/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": id},
		 async: false,
		 success: function(data,result)
		 {
			 if("SUCCESS" == data){
				 messageAlert(1,"删除患者诊断治疗记录成功",function()
				 {
						this.close();
						choose_close();
						showTreatPageDataList(1);
				 });
			 }
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除患者诊断治疗记录失败",function(){
						this.close();
						choose_close();
						showTreatPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	判断批量删除患者诊断治疗记录是否为空
 */
function del_treat(cbxName)
{
	var ids = getSelectCbx(cbxName);
	if(ids == "")
	{
		messageAlert(2,"请选择要删除的患者诊断治疗记录！",function(){choose_close(),this.close()});
	}
	else
	{
		messageAlert(4,'你确定要删除选择的患者诊断治疗记录吗？',function(){choose_open,deleteByIds(ids)},1)
	}
}

/**
 *	批量删除患者诊断治疗记录 
 */
function deleteByIds(ids){
	$.ajax({
		 url : projectName + "/treat/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": ids},
		 async: false,
		 success: function(data,result){
			 if("SUCCESS" == data){
				 messageAlert(1,"删除患者诊断治疗记录成功",function(){
						this.close();
						choose_close();
						showTreatPageDataList(1);
				 });
			 }
			 
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除患者诊断治疗记录失败",function(){
						this.close();
						choose_close();
						showTreatPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	编辑患者诊断治疗记录 
 */
function treatEdit(id)
{
	$.ajax({
		url : projectName+"/treat/getTreatById.do?date=" + new Date().getTime(),
		async : false,
		data:{"id":id},
		success : function(data) {
			if(null != data&&data != "")
			{
				clearTreatDialog();
				$("#treat_id").val(data.id);
				$("#treat_mrn").val(data.mrn);
				$("#treat_eno").val(data.eno);
				$("#treat_therary").val(data.therary);
				$("#treat_t_date").val(data.t_dateString);
				$("#treat_memo").val(data.memo);
				choose_open();
				addTreatDialog.set("title","编辑患者诊断治疗记录");
				addTreatDialog.show();		
			}
			else
			{
				messageAlert(0,"该患者诊断治疗记录已被删除！",function(){
					choose_close(),
					this.close(),
					showTreatPageDataList(1)
					});
			}
		}
	});
	
}


/***************************************************************
							患者诊断治疗记录分页
***************************************************************/
/**
 *	患者诊断治疗记录分页对象 
 */
var treatPageInfo={
		orderBy:null,
		order:"desc",
		pageNo:"1",
};


/**
 * 查询患者诊断治疗记录数据
 */
function showTreatPageDataList(pageNo)
{
	treatPageInfo.pageNo = pageNo;
	var search_mrn = getSearch_mrn("treat");
	var startDate = $("#treat_startDate").val();
	if(undefined == startDate||null == startDate)
	{
		startDate = "";
	}
	var endDate = $("#treat_endDate").val();
	if(undefined == endDate||null == endDate)
	{
		endDate = "";
	}
	var treatPageInfoString = JSON.stringify(treatPageInfo);
	window.location.href = projectName + "/treat/getPageTreat.do?PageInfo=" + treatPageInfoString + "&search_mrn=" + search_mrn + "&startDate=" + startDate +"&endDate=" + endDate;
}


/***************************************************************
							表单验证
***************************************************************/
var treatFormValidate;
function treatValidate()
{
	treatFormValidate = $("#treatForm").validate({
		errorPlacement: function(error, element) { 
         	error.appendTo(element.next());
         },
		rules:{
			mrn:
			{
				required:true,
				theTrueMrn:true
			},
			eno: 
			{
				required:true,
				theTrueEno:true
			},
			t_date_String:
			{
				required:true,
			}
		},
		messages:{
			mrn:
			{
                required:"患者病历号不能为空",
            },
            eno:
            {
            	required:"医师职工号不能为空",
            },
            t_date_String:
			{
				required:"不能为空",
			},
        }
		
	});
	
}







